package com.itita.weibo.ui;

import java.util.HashMap;
import java.util.List;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.FavoriteAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;

import weibo4andriod.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;


/**
 * 显示收藏的列表
 * 
 * @author 树朾 2010-4-24 21：15
 * 
 */

public final class ListMyFavoriteActivity extends Activity implements IWeiboActivity{


	public static final String REF_WEIBO="REF_WEIBO";//更新微博 
    
    public static final String REF_ICON="REF_ICON";//更新头像
    
    public static final String REF_WEIBO_MORE="REF_WEIBO_MORE";//更多信息
    
    public static final String REF_NEW_WEIBO_RESULT="REF_NEW_WEIBO_RESULT";//发表微博的结果
   
    public static final String REF_NEW_WEIBO_COMM_RESULT="REF_NEW_WEIBO_RESULT";//发表微博的结果
   
    //加载微博中的图片
    public static final String REF_GET_WEIBO_IMAGE = "REF_GET_WEIBO_IMAGE";
    
    ListView lv;
    View process;
    public int pagesize=5;
    public int nowpage=1;
    
    protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	    MainService.addActivity(this);
	    
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	    this.setContentView(R.layout.favorite);
	    
	    this.initControlsAndAddControlsEvent();
	}
    
     
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo am=(AdapterContextMenuInfo)item.getMenuInfo();
		
		switch(item.getItemId())
	    {
		case 1://
	    	Toast.makeText(this, "您要转发"+am.id,1000).show();
	    	break;
	    case 2://
	    	Toast.makeText(this, "您要评论列表第"+am.position+"项",1000).show();
	    	break;
	    case 3://
	    	Toast.makeText(this, "您要收藏",1000).show();
	    	break;
	    case 4://
	    	Toast.makeText(this, "您要查看信息",1000).show();
	    	break;
	    }
		return super.onContextItemSelected(item);
	
	
	}
	 
	public void onContextMenuClosed(Menu menu) {
   }
	
	//初始化上下文菜单
	 
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo am=(AdapterContextMenuInfo)menuInfo;
		
		if(am.position==0)//刷新
		{
			return;
		}
		if(am.id==-1)//更多
		{
			return;
		}
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("微博功能操作");
		//      组编号 项编号 排列顺序
		menu.add(1, 1, 1, "转发");
		menu.add(1, 2, 2, "评论");
		menu.add(1, 3, 3, "收藏");
		menu.add(1, 4, 4, "查看用户信息");
		
	}
	 
	@Override
	protected void onResume() {
		super.onResume();
		init();
	}
	public void init() {
		//请求当前用户的微博主页 
		HashMap<String, Integer> hm=new HashMap<String, Integer>();
		hm.put("pagesize", pagesize);
		hm.put("nowpage", nowpage); //请求当前用户的微博主页 
		Task tsHome=new Task(Task.TASK_GET_USER_FAVORITE,hm);
		
	    MainService.newTaskToTaskQueneHead(tsHome);
		
	}
	 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("keydown", ".................."+keyCode);
		this.finish();
		return true;
	}
	 
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals("REF_WEIBO")){  
			process.setVisibility(View.GONE);
	        if(param[1] == null){
	        	WeiboUtil.myToastShow(this, "加载数据失败");
	        	return;
	        }
			@SuppressWarnings("unchecked")
			List<Status> alls=(List<Status>)param[1];
			FavoriteAdapter ma=new FavoriteAdapter(this,alls);
			lv.setAdapter(ma);//更新列表
			
		}
		else if(flag.equals(REF_ICON)){ 
			//让数据适配器getView方法重新调用
			((FavoriteAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
		else if(flag.equals(REF_WEIBO_MORE)){
			@SuppressWarnings("unchecked")
			List<Status> alls=(List<Status>)param[1];
			((FavoriteAdapter)lv.getAdapter()).addMoreData(alls);
		}
		 
		
		 
	    //刷新微博中的图片
		else if(flag.equals(REF_GET_WEIBO_IMAGE)){
			//让数据适配器getView方法重新调用
			((FavoriteAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
	}

	@Override
	public void initControlsAndAddControlsEvent() {
		  //获取标题
	    View titleview= this.findViewById(R.id.title4Fav);
	    TextView tv=(TextView)titleview.findViewById(R.id.textView);
//	    当前用户的昵称
	    tv.setText("我的收藏");
	    tv.setTextSize((float) 16.0);
	    
//	    final ImageView imageViewTitle = (ImageView) titleview.findViewById(R.id.imageViewTitle);
//	    imageViewTitle.setImageResource(R.drawable.title_favourites);
	    
	  
	    
	    Button btnReturn=(Button)titleview.findViewById(R.id.title_bt_left);
//	    btnReturn.setText(R.string.imageviewer_back);
	    btnReturn.setBackgroundResource(R.drawable.title_back);
	    btnReturn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				ListMyFavoriteActivity.this.finish();
			}
	    }
	    );
	    
	    Button btnRefresh=(Button)titleview.findViewById(R.id.title_bt_right);
	    btnRefresh.setBackgroundResource(R.drawable.title_reload);
	    
	    btnRefresh.setOnClickListener(new OnClickListener(){   
			
			 @Override
			 public void onClick(View v) {
				//请求当前用户的微博主页 
				HashMap<String, Integer> hm=new HashMap<String, Integer>();
				hm.put("pagesize", pagesize);
				hm.put("nowpage", nowpage); //请求当前用户的微博主页 
				Task tsHome=new Task(Task.TASK_GET_USER_FAVORITE,hm);
				
			    MainService.newTaskToTaskQueneHead(tsHome);
			 }
	    });
	    
	    //获取列表对象和进度条对象
	    process	= this.findViewById(R.id.progress4Fav); 
	    lv = (ListView)this.findViewById(R.id.listview4Fav);
	    lv.setClickable(true);
	    
	    
	    //对ListView进行事件处理
	    lv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				// TODO Auto-generated method stub
				WeiboUtil.myToastShow(ListMyFavoriteActivity.this, "查看");
				if(arg3==-1){  //更多
					nowpage++;
					HashMap<String, Integer> hm=new HashMap<String, Integer>();
					hm.put("pagesize", pagesize);
					hm.put("nowpage", nowpage);   
					Task tsHome=new Task(Task.TASK_GET_USER_FAVORITE, hm);
					MainService.addNewTask(tsHome);
				}
				else if(arg3==0)
				{
					HashMap<String, Integer> hm=new HashMap<String, Integer>();
					hm.put("pagesize", pagesize);
					hm.put("nowpage", nowpage); 
			    	Task tsHome=new Task(Task.TASK_GET_USER_FAVORITE,hm);
			    	MainService.addNewTask(tsHome);
				}else{
				    Intent it=new Intent(ListMyFavoriteActivity.this,ViewWeiboActivity.class);
				   try{
					    FavoriteAdapter ma=(FavoriteAdapter)lv.getAdapter();
					    Status statu=(Status)ma.allstatus.get(arg2-1);
					    it.putExtra("status",statu);
				    }catch(Exception e){
				    	Log.e("e","error"+e);
				    }
//				    
				    ListMyFavoriteActivity.this.startActivityForResult(it, 0);
				}
				
			}
	    	
	    });
	    //注册上下文菜单
        this.registerForContextMenu(lv);
		
	}

}
